<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="管理员uid" prop="adminUid">
      <el-input v-model="dataForm.adminUid" placeholder="管理员uid"></el-input>
    </el-form-item>
    <el-form-item label="用户uid" prop="userUid">
      <el-input v-model="dataForm.userUid" placeholder="用户uid"></el-input>
    </el-form-item>
    <el-form-item label="用户uid" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户uid"></el-input>
    </el-form-item>
    <el-form-item label="操作的资源uid" prop="resourceUid">
      <el-input v-model="dataForm.resourceUid" placeholder="操作的资源uid"></el-input>
    </el-form-item>
    <el-form-item label="动作码" prop="actionCode">
      <el-input v-model="dataForm.actionCode" placeholder="动作码"></el-input>
    </el-form-item>
    <el-form-item label="动作名称" prop="actionName">
      <el-input v-model="dataForm.actionName" placeholder="动作名称"></el-input>
    </el-form-item>
    <el-form-item label="旧的积分数" prop="oldCredits">
      <el-input v-model="dataForm.oldCredits" placeholder="旧的积分数"></el-input>
    </el-form-item>
    <el-form-item label="新的积分数" prop="newCredits">
      <el-input v-model="dataForm.newCredits" placeholder="新的积分数"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="变更的积分" prop="changeCredits">
      <el-input v-model="dataForm.changeCredits" placeholder="变更的积分"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          adminUid: '',
          userUid: '',
          userName: '',
          resourceUid: '',
          actionCode: '',
          actionName: '',
          oldCredits: '',
          newCredits: '',
          remark: '',
          status: '',
          createTime: '',
          updateTime: '',
          changeCredits: ''
        },
        dataRule: {
          adminUid: [
            { required: true, message: '管理员uid不能为空', trigger: 'blur' }
          ],
          userUid: [
            { required: true, message: '用户uid不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户uid不能为空', trigger: 'blur' }
          ],
          resourceUid: [
            { required: true, message: '操作的资源uid不能为空', trigger: 'blur' }
          ],
          actionCode: [
            { required: true, message: '动作码不能为空', trigger: 'blur' }
          ],
          actionName: [
            { required: true, message: '动作名称不能为空', trigger: 'blur' }
          ],
          oldCredits: [
            { required: true, message: '旧的积分数不能为空', trigger: 'blur' }
          ],
          newCredits: [
            { required: true, message: '新的积分数不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          changeCredits: [
            { required: true, message: '变更的积分不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/creditslog/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.adminUid = data.creditsLog.adminUid
                this.dataForm.userUid = data.creditsLog.userUid
                this.dataForm.userName = data.creditsLog.userName
                this.dataForm.resourceUid = data.creditsLog.resourceUid
                this.dataForm.actionCode = data.creditsLog.actionCode
                this.dataForm.actionName = data.creditsLog.actionName
                this.dataForm.oldCredits = data.creditsLog.oldCredits
                this.dataForm.newCredits = data.creditsLog.newCredits
                this.dataForm.remark = data.creditsLog.remark
                this.dataForm.status = data.creditsLog.status
                this.dataForm.createTime = data.creditsLog.createTime
                this.dataForm.updateTime = data.creditsLog.updateTime
                this.dataForm.changeCredits = data.creditsLog.changeCredits
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/creditslog/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'adminUid': this.dataForm.adminUid,
                'userUid': this.dataForm.userUid,
                'userName': this.dataForm.userName,
                'resourceUid': this.dataForm.resourceUid,
                'actionCode': this.dataForm.actionCode,
                'actionName': this.dataForm.actionName,
                'oldCredits': this.dataForm.oldCredits,
                'newCredits': this.dataForm.newCredits,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'changeCredits': this.dataForm.changeCredits
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
